package Control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import View.DatabaseConnection;

/**
 * 
 * @author stephenbwilliamson
 */
public class ConnectDatabase implements ActionListener {

	private String databaseName;
	private String server;
	private String username;
	private String password;

	@Override
	public void actionPerformed(ActionEvent ev) {
		DatabaseConnection dbFrame = DatabaseConnection.getInstance();
		databaseName = dbFrame.jtDatabase.getText();
		server = dbFrame.jtServerURL.getText();
		username = dbFrame.jtUsername.getText();
		password = "";
		char[] pass = dbFrame.jpPassword.getPassword();
		for (int i = 0; i < pass.length; i++) {
			password += pass[i];
		}
		Arrays.fill(pass, '0');

		if (DataConnection.getInstance().tryConnection(server, databaseName,
				username, password)) {
			dbFrame.jlError.setText("Connection Successful");
			DatabaseConnection.getInstance().unload();
//			GeneVisualization.getInstance().load();

		} else {
			dbFrame.jlError.setText("Error while connecting");
		}
	}
}
